<?php


namespace App\Services\Business\Models;

use App\Services\Business\Models\Base;
use App\Services\Business\Models\Comment;
use App\Services\Business\Models\ThumbsUp;
use App\Services\Member\Models\User;
use App\Services\Member\Models\Activity;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;


class Run extends Base
{


    protected $table = 'run';



    protected $fillable = [
        'user_id' ,'title','step','comments_count'
    ];
    protected $appends = ['isThumbsup'];

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }




    public  function  activity()
    {
        return $this->morphMany(Activity::class, 'activityable');
    }


    public  function  redpacketRecords()
    {
        return $this->hasMany(UserRedpacketRecord::class, 'run_id');

    }

    public function images()
    {
        return $this->morphMany(Image::class, 'imageable');
    }


    public function videos()
    {
        return $this->morphToMany(Video::class, 'videoable');
    }

    public function  redpacketRecord()
    {

        return $this->hasMany(UserRedpacketRecord::class, 'run_id');
    }

    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }

    public function getIsThumbsupAttribute()
    {
        $userId = Auth::guard('apiv2')->id(); // 获取当前登录用户
        $isThumbsup = false;
        if ($userId) {
            return $this->thumbsups->contains('user_id', $userId);
        }
        return $isThumbsup;
    }

    public function thumbsups()
    {
        return $this->morphMany(ThumbsUp::class, 'thumbsupable')->with(['user:id,avatar']);
    }





}
